using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_UserProfileEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        UserProfile userProfile;

        if (Int32.TryParse(Request["id"], out id))
            userProfile = UserProfile.Load(id);
        else
            userProfile = null;

        if (userProfile != null)
        {
            if (!string.IsNullOrEmpty(userProfile.FirstName))
				tbxFirstName.Text = userProfile.FirstName;
			if (!string.IsNullOrEmpty(userProfile.LastName))
				tbxLastName.Text = userProfile.LastName;
			tbxUserAddressId.Text = userProfile.UserAddressId.ToString();
			tbxBillingAddressId.Text = userProfile.BillingAddressId.ToString();
			tbxShippingAddressId.Text = userProfile.ShippingAddressId.ToString();
			if (!string.IsNullOrEmpty(userProfile.SelectedTheme))
				tbxSelectedTheme.Text = userProfile.SelectedTheme;
			tbxSelectedTimeZone.Text = userProfile.SelectedTimeZone.ToString();
			tbxGiftCouponBalance.Text = userProfile.GiftCouponBalance.ToString();
			tbxProfileForSiteuserId.Text = userProfile.ProfileForSiteuserId.ToString();
			if (userProfile.DateOfBirth != SqlDateTime.MinValue)
				tbxDateOfBirth.Text = userProfile.DateOfBirth.ToShortDateString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string firstName = Security.BindStringInput(tbxFirstName.Text, _securityLevel);
		string lastName = Security.BindStringInput(tbxLastName.Text, _securityLevel);
		int userAddressId = Security.BindIntegerInput(tbxUserAddressId.Text, _securityLevel);
		int billingAddressId = Security.BindIntegerInput(tbxBillingAddressId.Text, _securityLevel);
		int shippingAddressId = Security.BindIntegerInput(tbxShippingAddressId.Text, _securityLevel);
		string selectedTheme = Security.BindStringInput(tbxSelectedTheme.Text, _securityLevel);
		int selectedTimeZone = Security.BindIntegerInput(tbxSelectedTimeZone.Text, _securityLevel);
		double giftCouponBalance = Security.BindDoubleInput(tbxGiftCouponBalance.Text, _securityLevel);
		int profileForSiteuserId = Security.BindIntegerInput(tbxProfileForSiteuserId.Text, _securityLevel);
		DateTime dateOfBirth = Security.BindDateTimeInput(tbxDateOfBirth.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (UserProfile.Save(
				id, 
				firstName,
				lastName,
				userAddressId,
				billingAddressId,
				shippingAddressId,
				selectedTheme,
				selectedTimeZone,
				giftCouponBalance,
				profileForSiteuserId,
				dateOfBirth
                ))
                    Response.Redirect("userprofilelist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            UserProfile userProfile = UserProfile.Create(
				firstName,
				lastName,
				userAddressId,
				billingAddressId,
				shippingAddressId,
				selectedTheme,
				selectedTimeZone,
				giftCouponBalance,
				profileForSiteuserId,
				dateOfBirth
                );

            if (userProfile != null)
                Response.Redirect("userprofilelist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("userprofilelist.aspx");
    }
}
